<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_block_front_HtmlEditAction extends m_backoffice_extends_BlockEditModal
{
	public function execute()
	{
        $formValues = $this->getFormValues('form');
        
        $block = $this->getBlock();

        $tempDocService = m_backoffice_services_TempDocService::getInstance();

        $content = '';
        if(isset($block['contentId']))
        {
            $htmlDoc = $tempDocService->getDocument($block['contentId']);
            $content = $htmlDoc->getContent();
        }

        if(count($formValues) > 0)
        {
            // document is new
            if(!isset($htmlDoc) || is_null($htmlDoc))
            {
                $htmlDoc = m_website_documents_Html::createNew();
                $html = $htmlDoc->setContent($formValues['html'])->setWebsiteId($this->getPage()->getWebsiteId())->save(false);
                $block['contentId'] = $html->getUniqueId();
            }
            else
            {
                $html = $htmlDoc->setContent($formValues['html'])->setWebsiteId($this->getPage()->getWebsiteId());
                $tempDocService->saveTemporaryDocument($html);
                $block['withValidation'] = 'true';
            }

            $this->saveBlock($block);
            $this->closeModal();
        }

        $this->addToModel('content', $content);
        $this->setModalParameters();
		$this->setView('success');
	}	
}